package com.eric.demo.netty.loopsend;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

/**
 * @author ZhangGuangyin on 2018/10/15
 */
public class TimeServerHandler extends ChannelHandlerAdapter {

    @Override

    public void channelActive(final ChannelHandlerContext ctx) throws InterruptedException { // (1)


        while (true) {

            ByteBuf data = ctx.alloc().buffer(4); //为ByteBuf分配四个字节

            data.writeInt((int) (System.currentTimeMillis() / 1000L + 2208988800L));

            ctx.writeAndFlush(data); // (3)

            Thread.sleep(2000);

        }

    }


    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {

        cause.printStackTrace();

        ctx.close();

    }
}
